<?php
namespace app\api\controller;
use think\Db;
use think\Controller;
use think\Session;

class ShaiTu extends Controller
{
    public function getList()
    {
        $result=db('shaitu')
        ->alias(['shaitu'=>'a','users'=>'b'])
        ->join('users','b.uid= a.uid')       
        ->field('a.sid,a.smessage,a.spic,a.sreleasetime,b.uname')
        ->paginate(10)
        ->each(function($item){
                    $num=db('discussion')->where('sid',$item['sid'])->count();
                    $item['num']=$num;
                    return $item;
                });
        if($result)
        {
            echo json_encode($result);

        }
        else
            echo json_encode(['0']);
        die();
    }
    public function getDetail(){
        $sid=input('id');
        $result=db('shaitu')
        ->alias(['shaitu'=>'a','users'=>'b','discussion'=>'c'])
        ->join('users','b.uid= a.uid')  
        ->join('discussion','c.sid= a.sid')
        ->where('a.sid',$sid)     
        ->field('a.sid,a.smessage,a.spic,a.sreleasetime,b.uname,c.did')
        ->paginate(10)
        ->each(function($item){
                    $list=db('discussion')
                    ->alias(['discussion'=>'a','users'=>'b'])
                    ->join('users','b.uid= a.uid')  
                    ->field('a.did,a.dtime,b.uname,a.dcontent')
                    ->where('sid',$item['sid'])
                    ->select();
                    $item['commentlist']=$list;
                    $list2=db('reply')   
                    ->alias(['reply'=>'a'])          
                    ->field('a.rid,a.rtime,a.rtype,a.replyid,a.rcontent,a.fromuid,a.touid')
                    ->where('did',$item['did'])
                    ->select();
                    $item['relylist']=$list2;
                    return $item;
                });
         if($result)
        {
            echo json_encode($result);

        }
        else
            echo json_encode(['0']);
        die();
    }
    public function release(){
        if(Session::has('uid')){
          $pic = request()->file('spic');
        if (!empty($pic)) 
        {
            $info = $pic->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static/img/shaitu');
            if ($info == false) {
                $this->$pic->getError();
            }
        $data=[
        'smessage'=>input('smessage'),
        'spic'=>$info->getSaveName(),
        'sreleasetime'=>date('Y-m-d H:i:s',time()),
        'uid'=>Session::get('uid'),
        ];
         if( Db::name("shaitu")->insert($data)){
           echo json_encode(["status"=>1,"message"=>"success"]);
        }
        else{
           echo json_encode(["status"=>0,"message"=>"fail1"]);
        }
        } 
        else 
        {
          echo json_encode(["status"=>0,"message"=>"fail2"]);
        }
        }
        else 
          echo json_encode(["status"=>0,"message"=>"not login"]);
    }
    public function delete(){
        $id=input('id');
        if(Db::table("shaitu")->delete($id)){
            echo json_encode(["status"=>1,"message"=>"success"]);
           }
        else{
           echo json_encode(["status"=>0,"message"=>"fail"]);
        }
    }
}